<?php
include_once("../../configure/configure.php");		//--> global var
include_once("../configure/admin.config.inc.php");	//--> admin global var
include_once("Smarty.class.php");					//--> out template
include_once("utils/FileUtils.php");				//--> Files
include_once("utils/ImageUtils.php");

include_once("impl/ContentImpl.php");
include_once("impl/ImageImpl.php");
include_once("impl/TagImpl.php");

require_once("controlHeader.php");					//--> system control header

$contImpl 	= new ContentImpl(DB_TAG_MM);		//--> Merchant
$imgImpl	= new ImageImpl(DB_TAG_MM);			//--> Files
$tagImpl	= new TagImpl(DB_TAG_MM);

$error_message 	= '';


$arrAllImage 		= array();
$arrExistedImage 	= array();


$imageid 	= $_GET['imageid'];

if(isset($_GET['act']))
	$act 	= $_GET['act'];
else if(isset($_POST['act']))
	$act 	= $_POST['act'];
	
	
if($act == 'delete' && !empty($imageid)){
	$oImg = $imgImpl->getById($imageid);
	
	if(is_object($oImg)){
		$imageid 	= $oImg->id;
		$imageName	= $oImg->extName;
		
		if($contImpl->isNotExisted($id, $imageid)){
			$path = FileUtils::getFullPathByID(IMAGE_PATH_CONTENT, $imageid, $imageid . '.' . $imageName);
			
			if (file_exists($path)) {
				unlink($path);
			}
			
			$imgImpl->delete($imageid);
		}
		
		$contImpl->deleteImage($id, $imageid);
		
		echo "<script language='javascript'>";
		echo "alert(\"delete success.\");";
		echo "location.href=\"".$_SERVER['HTTP_REFERER']."\";";
		echo "</script>";
	}
}



$name		= '';
$summary	= '';
$status		= '1';
$level		= '10';

if(isset($_POST['Submit'])){
	$name			= trim($_POST['name']);
	$description	= trim($_POST['description']);
	$status			= $_POST['status'];
	$level			= $_POST['level'];
	$tags			= $_POST['tags'];
	
	$arrImages = array();

	$blacklist = array(".php", ".phtml", ".php3", ".php4");
	
	if(!is_array($_FILES["image"]))
		$error_message = "please select upload image.";
	else{
		foreach ($_FILES["image"]["error"] as $key => $error) {
	    	if ($error == UPLOAD_ERR_OK) {
				foreach ($blacklist as $item) {
					if(preg_match("/$item\$/i", $_FILES['image']['name'][$key])) {
					 	$error_message = "only support gif and jpg format.";
					}
				}
				
				if(empty($error_message)){
					$imageinfo = getimagesize($_FILES['image']['tmp_name'][$key]); 
		
					if($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg' && $imageinfo['mime'] != 'image/png') { 
						$error_message = "only support gif and jpg format.";
					}else if($file_size >8000000)
						$error_message = 'Your image more than 8000 kb('.ceil($file_size/1024).'kb)';
					else{
						/*-- get ext image file name --*/
						$arr_image_extend_name 	= explode('/', $imageinfo['mime']);
						$image_extend_name 		= $arr_image_extend_name[count($arr_image_extend_name) - 1];
						
						$arrImage = array();
						$arrImage['name'] 		= $_FILES['image']['name'][$key];
						$arrImage['tmp_name'] 	= $_FILES['image']['tmp_name'][$key];
						$arrImage['file_size']	= filesize($_FILES['image']['tmp_name'][$key]);
						
						$arrImage['width']		= $imageinfo[0];
						$arrImage['height']		= $imageinfo[1];
						
						$arrImage['md5']		= md5(file_get_contents($_FILES['image']['tmp_name'][$key]));
						$arrImage['extName']	= $image_extend_name;
						
						$arrImages[] 			= $arrImage;
					}
				}		
	    	}
	    	
	    	if(!empty($error_message)){
	    		break;
	    	}
		}
	}
	
	if(count($arrImages) < 1)
		$error_message = "please select upload image.";
		
	foreach ($arrImages as $arrImg) {
		$dto = $imgImpl->getByMd5($arrImg['md5']);
		if(is_object($dto)){
//			$contImpl->addImage($id, $dto->id);
			
			$error_message = "Image existed. Image md5 " . $arrImg['md5'];
		}else{			
			$dto = new ImageDto();
			
			$dto->width			= $arrImg['width'];
			$dto->height		= $arrImg['height'];
			$dto->extName		= $arrImg['extName'];
			$dto->fileSize		= $arrImg['file_size'];
			$dto->md5			= $arrImg['md5'];
			
			$dto->name 			= $name;
			$dto->description	= $description;
			$dto->status		= $status;
			$dto->level			= $level;
			$dto->isAnimated	= ImageUtils::isAnimatedGif($arrImg['tmp_name']);
			
			$imageid = $imgImpl->add($dto);
			
			if($imageid > 0){
				$contImpl->addImage($id, $imageid);
				
				$imageName = $imageid . '.' . strtolower($arrImg['extName']);
				
				if (!move_uploaded_file($arrImg['tmp_name'], FileUtils::getFullPathByID(IMAGE_PATH_CONTENT, $imageid, $imageName))) {//-- save image
					$error_message = "add new image failure.";
				}else
					$error_message = "add new image success.";
					
				$imgImpl->delTags($imageid);
				$tagIds = $tagImpl->addTags($tags);
				if(is_array($tagIds)){
					foreach($tagIds as $tagId)
						$imgImpl->addTag($imageid, $tagId);
				}
			}else
				$error_message = "add new image failure.";
		}
	}
}

$arrStatus = array('1' => 'Online', '0' => 'Offline');


$oImages 	= $contImpl->getImages($id, 'ORDER BY level DESC');
foreach($oImages as $oImage){
	$oImage->tags = $imgImpl->getTags($oImage->id);
}

$oContent 	= $contImpl->getById($id);

/*----- out html -----*/
$smarty = new Smarty();	//----- out template
$smarty->template_dir = TEMPLATE_SYS_DIR;
$smarty->compile_dir  = CACHE_SYS_DIR;

$smarty->assign('error_message', $error_message);
$smarty->assign('menuGid', $menuGid);
$smarty->assign('menuid', $menuid);
$smarty->assign('backurl',$backurl);

$smarty->assign('id',$id);

$smarty->assign('oImages', $oImages);
$smarty->assign('oContent', $oContent);

$smarty->assign('arrStatus', $arrStatus);
$smarty->assign('status', $status);

//$smarty->assign('name', $name);
//$smarty->assign('level', $level);

//$smarty->assign('description', $description);


$smarty->display('listContent_Images.htm');
?>
